//
//  XOShopsViewController.m
//  XiOuMall
//
//  Created by lifayu on 2018/2/11.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "XOShopsViewController.h"
#import "EJSCategoryTableViewCell.h"
#import "XOShopHeaderCollectionReusableView.h"
#import "XOShopFooterCollectionReusableView.h"
#import "XOShopGoodsCollectionViewCell.h"
#import "XOShopDetailViewController.h"
#import "EJSGoodsDetailInfoViewController.h"

#import "XOPlatformCategoryListRequest.h"
#import "XOShopListRequest.h"
#import "XOShopTabBarController.h"
#import "XOBussinnesCategoryListRequest.h"
#import "XOBussinessCategoryModel.h"
#import "XOShopViewModel.h"
@interface XOShopsViewController ()<XOShopHeaderCollectionReusableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) NSMutableArray *shopCategoryList;
@property (strong, nonatomic) XOBussinessCategoryModel * selectCategoryModel;
@property(nonatomic, assign) int flag;
@end

@implementation XOShopsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"店铺";
    self.flag = -1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden= NO;

    if(self.shopCategoryList.count == 0){
        [self getShopCategoryList];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
        if (self.flag==0) {
            self.navigationController.navigationBar.hidden= YES;
        }
}

- (void)configOwnViews
{

    [self configTableView];
    [self configCollectionView];
}

- (void)configTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSCategoryTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSCategoryTableViewCell"];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.tableView setBounces:YES];
    [self.tableView setAlwaysBounceVertical:YES];
}

- (void)configCollectionView
{
    [self.collectionView registerNib:[UINib nibWithNibName:@"XOShopGoodsCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"XOShopGoodsCollectionViewCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"XOShopHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"XOShopHeaderCollectionReusableView"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"XOShopFooterCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"XOShopFooterCollectionReusableView"];
    [self.collectionView setAlwaysBounceVertical:YES];
}

#pragma mark - request data
//获取店铺分类
- (void)getShopCategoryList{
    FlyWeakSelf
    XOBussinnesCategoryListRequest *req = [XOBussinnesCategoryListRequest new];
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        /* 需要修改 */
        NSMutableArray * tempArray = [NSMutableArray arrayWithArray:response.data];
        weakSelf.shopCategoryList = tempArray;
       XOBussinessCategoryModel* tempObj = [[XOBussinessCategoryModel alloc]init];
        tempObj.categoryName = @"全部商铺";
        tempObj.categoryId = @"";
        [weakSelf.shopCategoryList insertObject:tempObj atIndex:0];
        [weakSelf.tableView reloadData];
        if(weakSelf.shopCategoryList.count > 0){
            [weakSelf hideNoDataView];
            [weakSelf.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
            weakSelf.selectCategoryModel =  weakSelf.shopCategoryList.firstObject;
        }else{
            [weakSelf showNoDataViewWithText:@"店铺列表为空"];
        }
    } failHandler:^(BaseResponse *response) {
        
    }];
}

- (void)setSelectCategoryModel:(XOBussinessCategoryModel *)selectCategoryModel
{    if(_selectCategoryModel != selectCategoryModel){
        _selectCategoryModel = selectCategoryModel;
        if(!_selectCategoryModel.shopList){
            [self getCategoryShopList];
        }else {
            [self.collectionView reloadData];
            self.collectionView.contentOffset = CGPointMake(0, 0);
        }
    }
}

- (void)getCategoryShopList{
    XOShopListRequest *req = [XOShopListRequest new];
    if (![self.selectCategoryModel.categoryName isEqualToString:@"全部商铺"]) {
        req.shop_category_id = self.selectCategoryModel.categoryId;
    }
    req.pageLimit = @"500";

    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        XOShopListResponseData *data = response.data;
        weakSelf.selectCategoryModel.shopList = [NSMutableArray arrayWithArray:data.list];
        [weakSelf.collectionView reloadData];
        weakSelf.collectionView.contentOffset = CGPointMake(0, 0);
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance]tipMessage: @"无法获取店铺"];
    }];

}

#pragma mark - TableView DataSource

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.shopCategoryList.count;
}

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    EJSCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSCategoryTableViewCell" forIndexPath:indexPath];
    XOBussinessCategoryModel *model = self.shopCategoryList[indexPath.row];
    cell.categoryNameLabel.text = model.categoryName;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 62;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    self.selectCategoryModel = self.shopCategoryList[indexPath.row];
}

#pragma mark - CollectionView DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.selectCategoryModel.shopList.count;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    XOShopModel *shopModel = [self.selectCategoryModel.shopList objectAtIndex:section];
    
    return shopModel.goods.count;
}

- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    XOShopGoodsCollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XOShopGoodsCollectionViewCell" forIndexPath:indexPath];
    XOShopModel *shopModel = [self.selectCategoryModel.shopList objectAtIndex:indexPath.section];
    EJSGoodsModel *goodsModel = shopModel.goods[indexPath.item];
    [ccell.goodsImageView setImageWithUrl:goodsModel.cover];
    ccell.goodsPriceLabel.text = [NSString stringWithFormat:@"€ %.2f",goodsModel.price.floatValue];
    return ccell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (collectionView.width - 30 - 20)/3;
    return CGSizeMake(width, width + 20);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        XOShopHeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"XOShopHeaderCollectionReusableView" forIndexPath:indexPath];
        headerView.delegate = self;
        XOShopModel *shopModel = [self.selectCategoryModel.shopList objectAtIndex:indexPath.section];
        [headerView setShopModel:shopModel];
        return headerView;
    }else{
        XOShopFooterCollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"XOShopFooterCollectionReusableView" forIndexPath:indexPath];
        return footerView;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(ScreenWidth, 70);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if(section == [self numberOfSectionsInCollectionView:self.collectionView] - 1){
        return CGSizeZero;
    }
    return CGSizeMake(ScreenWidth, 16);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    XOShopModel *shopModel = [self.selectCategoryModel.shopList objectAtIndex:indexPath.section];
    
    XOShopTabBarController *shopTabVc = [[XOShopTabBarController alloc] init];
    [XOShopViewModel shared].checkShopIndex = 0; //去商品
    [XOShopViewModel shared].shopModel = shopModel;
    self.flag = 0;
    [self.navigationController pushViewController:shopTabVc animated:YES];

}

#pragma mark - XOShopHeaderCollectionReusableViewDelegate
- (void)entryShopAction:(XOShopModel *)shopModel{
    XOShopTabBarController *shopTabVc = [[XOShopTabBarController alloc] init];
    [XOShopViewModel shared].shopModel = shopModel;
    [XOShopViewModel shared].checkShopIndex = 0;//去商品

    self.flag = 0;
    [self.navigationController pushViewController:shopTabVc animated:YES];
}


@end
